package com.coe3.data;

import java.io.File;
import java.io.IOException;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

import com.daydr3am.thenation.R;

public class PathManager {
	
	public static final String Extension_ZIP = ".zip";
	public static final String Extension_JPG = ".jpg";
	public static final String Extension_PDF = ".pdf";
	
	public static String combineExtension(String filename, String extension) {
		return filename + extension;
	}
	
	public static String removeExtension(String filename) {
		// Handle null case specially.

        if (filename == null) return null;

        // Get position of last '.'.

        int pos = filename.lastIndexOf(".");

        // If there wasn't any '.' just return the string as is.

        if (pos == -1) return filename;

        // Otherwise return the string, up to the dot.

        return filename.substring(0, pos);
	}
	
//	public static Boolean deleteFile(String filename) {
//		File file = new File(filename);
//		if(file.exists())
//			return file.delete();
//		else
//			return false;
//	}
	
	public static void DeleteRecursive(File fileOrDirectory) {
	    if (fileOrDirectory.isDirectory())
	        for (File child : fileOrDirectory.listFiles())
	            DeleteRecursive(child);

	    fileOrDirectory.delete();
	}
	
	public static void mkdir(Context context, Boolean isPDF) throws IOException {

        File file = new File(getExternalCacheDir(context, isPDF));
        
        if (!file.exists() || !file.isDirectory())
        	Log.d("chud", "file.mkdir(): " + file.mkdirs());
    }
    
    public static String getExternalCacheDir(Context context, Boolean isPDF) {
        String cacheDir = "/Android/data/" 
        						+ context.getPackageName() 
        						+ "/cache/";
        if(isPDF)
        	cacheDir += (context.getString(R.string.str_special_issues) + File.separator);
        else
        	cacheDir += (context.getString(R.string.str_avaliable_issues) + File.separator);
        
        return Environment.getExternalStorageDirectory().getPath() + cacheDir;
    }
}
